import { useSelector, useDispatch } from "react-redux"
import { useCallback } from "react";
import CommodityApi from "../api/commodity"
import Creators from "../store/actions";
import { useAdd_info } from './add_commodityImage';
import { useHistory } from "react-router"

import { useAssType } from "./ass_type";
import typeApi from "../api/totalType"


export const useCommodity = () => {
    // 从状态机中获取参数
    const commodity = useSelector(state => state.commodity);
    const dispatch = useDispatch();
    const { curPage, eachPage } = commodity;
    const [, { addCommodityImage }] = useAdd_info();

    // 
    const [{
        ChildAll,
        TotalAl,
        assType
    }, { getassType, addAssTypeToReducer }] = useAssType()

    const history = useHistory();

    //--------------------------------------
    const seckill = useSelector(state => state.seckill_add)
    const { commodity_id, name, Price, restricted,
        Total, Sold, Start_time, End_time, Sale_type, State
    } = seckill
    // 将要新增的数据放入状态机
    const addSeckills = useCallback((data) => {
        // 将拿到的对象传入状态机

        dispatch(Creators.seckillAddSeckill(data))
    }, [commodity_id, name, Price, restricted, Total, Sold, Start_time, End_time, Sale_type, State])
    //--------------


    // 获取商品数据
    const getCommodity = useCallback(async () => {
        // 调用接口获取数据
        const data = await CommodityApi.get({ curPage, eachPage });
        // 将通过接口获取到的数据合并到总数据中去
        const result = await typeApi.getassType()
        addAssTypeToReducer(result)

        dispatch(Creators.getCommodity(data));
    }, [curPage, eachPage]);

    //新增商品数据


    // 
    const commodityAdd = useCallback(async date => {

        // 调用新增接口
        const data = await CommodityApi.add(date);

        // 在这里应用  另外一个  hook   将返回的数据添加到甘冈定义的状态机
        // addCommodityImage(data);
        // return data
        // 将通过接口获取到的数据合并
        dispatch(Creators.addCommodity(data));
        history.push("/info/commodity")
    }, [dispatch]);

    // 删除商品数据
    const delCommodity = useCallback(async _id => {
        const data = await CommodityApi.del(_id);
        return data;
    })

    // 通过名称查找商品
    const findCommodity = useCallback(async name => {
        const rows = await CommodityApi.find(name);
        dispatch(Creators.findCommodity({ rows }));
    }, [dispatch]);

    // 修改商品数据
    const editCommodity = useCallback(async (params) => {
        const data = await CommodityApi.edit(params);
        dispatch(Creators.editCommodity(data));
    }, [dispatch]);

    // 设置商品当前页
    const setCurPage = useCallback(curPage => {

        dispatch(Creators.commoditySetCurPage({ curPage }))

    }, [dispatch]);

    // 设置商品每页显示条数
    const setEachPage = useCallback(eachPage => {
        dispatch(Creators.commoditySetEachPage({ eachPage }))
    }, [dispatch]);
    // 修改类型
    const resComType = useCallback(async (info) => {

        const result = await CommodityApi.resComType(info)

        if (result.ok === "1") {
            const data = await CommodityApi.get({ curPage, eachPage });
            dispatch(Creators.getCommodity(data));
        }

        return result

    }, [dispatch])

    // 将数据 和 方法 暴露出去
    return [commodity, { getCommodity, commodityAdd, delCommodity, findCommodity, editCommodity, setCurPage, setEachPage, addSeckills, resComType }];
    // return [commodity, { getCommodity, addCommodity, delCommodity, findCommodity, editCommodity,  }];
}